package com.gcsj.service.impl;

import com.gcsj.mapper.UserMapper;
import com.gcsj.pojo.User;
import com.gcsj.service.UserService;
import com.gcsj.vo.PageRequest;
import com.gcsj.vo.PageResult;
import com.gcsj.vo.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public int addUser(User user) {
        //TODO  l
        return userMapper.addUser(user);
    }

    @Override
    public Object login(User user) {
        List<User> users = userMapper.selectUserList(user);
        if(users.size() >= 2){
            return "该账号重复，请输入正确账号！";
        }else if(users.size() == 0){
            return "无此账号！";
        } else{
            User loginUser = users.get(0);
            if (loginUser.getPassword().equals(user.getPassword())) {
                return loginUser;
            }else{
                return "密码错误！";
            }
        }
    }

    @Override
    public List<User> getUserList(User user) {
        return userMapper.selectUserList(user);
    }

    @Override
    public User getUserByName(String nickname) {
        return userMapper.selectUserByName(nickname);
    }

	@Override
    public User getUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public int updateAvatar(User user) {
        return userMapper.updateAvatar(user);
    }

    @Override
    public PageResult listUser(PageRequest pageRequest) {

        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));

    }

    @Override
    public PageResult selectUserListBy(PageRequest pageRequest, User user) {

        return PageUtils.getPageResult(pageRequest, getPageInfo1(pageRequest,user));

    }

    public PageInfo<User> getPageInfo(PageRequest pageRequest) {

        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);//核心代码:pageHelper查询分页
        List<User> user = userMapper.listUser();
        PageInfo<User> pageInfo = new PageInfo<>(user);
        return pageInfo;
    }

    public PageInfo<User> getPageInfo1(PageRequest pageRequest, User user) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<User> user1 = userMapper.selectUserListByPage(user);
        PageInfo<User> pageInfo1 = new PageInfo<>(user1);
        return pageInfo1;
    }
}
